package com.sky.service.impl;

import com.sky.constant.ShopConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import jdk.jshell.Snippet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 获取营业状态
     *
     * @return 营业状态
     */
    @Override
    public Integer getStatus() {
        return (Integer) redisTemplate.opsForValue().get(ShopConstant.SHOP_STATUS_REDIS_KEY);
    }

    /**
     * 设置营业状态
     *
     * @param status 营业状态
     */
    @Override
    public void update(Integer status) {
        redisTemplate.opsForValue().set(ShopConstant.SHOP_STATUS_REDIS_KEY, status);
    }

    /**
     * 添加商品到购物车
     *
     * @param dto 商品dto
     */
    @Override
    public void addToShoppingCart(ShoppingCartDTO dto) {
        // 1.先根据商品id查询
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(dto.getDishId())
                .setmealId(dto.getSetmealId())
                .dishFlavor(dto.getDishFlavor())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 2.如果有数据,更新操作 (商品数量加一)
        if (list.size() > 0) {
            ShoppingCart dbCart = list.get(0);
            dbCart.setNumber(dbCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(dbCart);
        } else {
            // 3.如果没有数据,添加操作
            // 判断是菜品还是套餐
            if (shoppingCart.getDishId() != null) {
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            if (shoppingCart.getSetmealId() != null) {
                SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 添加操作
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return 商品集合
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param dto dto
     */
    @Override
    public void delete(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(dto.getDishId())
                .setmealId(dto.getSetmealId())
                .dishFlavor(dto.getDishFlavor())
                .build();
        //  - 查询当前用户，当前这个商品（菜品+口味、套餐）在购物车数据。
        //  - 判断其购物车的商品数量，如果是1个，就直接将其删除。
        //  - 如果数量超过1个，就将其数量减1，然后再更新到数据库中。
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        Integer number = cart.getNumber();
        if (number == 1) {
            shoppingCartMapper.delete(cart);
        } else if (number > 1) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }
}